# encoding: utf-8

import time
import uuid
import datetime
import re
import json
from hashlib import sha1, sha256

import scrapy
from scrapy import Request, FormRequest

try:
    from App_Spd.items import AppSpdItem
    from App_Spd.utils import analysis, get_urlid, get_pubtime, timestamp_now_to
except:
    from app_spider.items import AppSpdItem
    from app_spider.utils import analysis, get_urlid, get_pubtime, timestamp_now_to


def singature(str, str2, str3, str4, sign_salt, str5):
    str_format = "{}&&{}&&{}&&{}&&{}&&{}".format(str, str2, str3, str4, sign_salt, str5)
    s1 = sha256()
    s1.update(str_format.encode("utf-8"))
    return s1.hexdigest()


def gen_headers(_t):
    headers = {}
    signature_salt = "FR*r!isE5W"
    _uid = str(uuid.uuid1())
    headers['X-SESSION-ID'] = '62453734010ef65c3e54f2f6'
    headers['X-REQUEST-ID'] = _uid
    headers['X-TIMESTAMP'] = _t
    headers['X-TENANT-ID'] = '41'
    headers['User-Agent'] = '1.1.6;ffffffff-cb81-b84d-ffff-ffff98c49000;HUAWEI TAS-AN00;Android;7.1.2;Release'
    headers['Cache-Control'] = 'no-cache'
    headers['Host'] = 'vapp.tmuyun.com'
    headers['Connection'] = 'Keep-Alive'
    headers['Accept-Encoding'] = 'gzip, deflate'
    headers['X-SIGNATURE'] = singature("/api/article/channel_list", headers['X-SESSION-ID'], _uid, _t,
                                       signature_salt, headers['X-TENANT-ID'])
    return headers


class YueyinqingyangAppSpider(scrapy.Spider):
    name = 'yueyinqingyang_app'

    websiteId = '2141422561'
    pubSource = '乐音清扬'
    list_url = 'https://vapp.tmuyun.com/api/article/channel_list?channel_id={}'
    detail_url = 'https://vapp.tmuyun.com/api/article/detail?id={}'

    channels = [
        '5fa78020b40eef61e464ee61',
        "5fbf59e9ad61a44fa854abce",
        "5fd03ff6b40eef4a49b2b2b5",
        "5de74f0b1b011b48a65b7646",
        "61c28a76fe3fc11c0870137d",
        "61b1a4dead61a42065f90198",
        "5fc4913aad61a44fa854bbdc",
        "62022761b40eef276728446a",
        "620227751798f3048d66989c",
        "6052c0c0ad61a43e70548e31",
        "60010098ad61a40583525056",
        "6013d219ad61a40583526db3",
    ]

    def start_requests(self):
        for chl in self.channels:
            _t = int(time.time() * 1e3)
            yield Request(url=self.list_url.format(chl), headers=gen_headers(_t))

    def parse(self, response, **kwargs):
        resp = response.json()
        # print(resp)
        for opt in ['focus_list', 'article_list']:
            datas = resp['data'][opt]
            for obj in datas:
                print(obj)
                # share_url = obj.get('share_url')
                # item = AppSpdItem()
                # item['websiteId'] = 2141422567  # 提供app对应的id
                # item['pubSource'] = "今日越城"  # 提供app对应名称
                # item['title'] = obj.get('doc_title', '')
                # item['author'] = obj.get('author', '')
                # item['pubtime'] = timestamp_now_to(obj.get('published_at', time.time()))
                # yield Request(url=share_url, headers=web_headers, callback=self.parse_detail, meta={'item': item})
